package com.zj.blessings.util;

import java.util.TimeZone;

import com.zj.blessings.Consts;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class PreferenceUtils {
	public static boolean isTimeToUpdate(Context context) {
		SharedPreferences sp = context.getSharedPreferences(Consts.SP_NAME,
				Context.MODE_PRIVATE);
		long localTime = sp.getLong(Consts.SP_KEY_LAST_TIME, 0l);
		long curTime = System.currentTimeMillis();
		if (daysBetween(curTime, localTime) > 1) {
			setTime(context);
			return true;
		}
		return false;
	}

	private static long daysBetween(long time1, long time2) {
		int offset = TimeZone.getDefault().getRawOffset();
		return (time1 + offset) / 86400000 - (time2 + offset) / 86400000;
//		if (time1 - time2 > 30000) {
//			return Integer.MAX_VALUE;
//		}
//		return 0;
	}

	private static void setTime(Context context) {
		SharedPreferences sp = context.getSharedPreferences(Consts.SP_NAME,
				Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putLong(Consts.SP_KEY_LAST_TIME, System.currentTimeMillis());
		editor.commit();
	}
}
